package com.iiapk.location;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

public class CallerLocationProvider extends ContentProvider {
	
	private final static String TAG = "CallerLocationProvider";

	final static String NUMBER_TABLE = "CallerLoc";
	final static String LOCATION_TABLE = "LocationInfo";
	final static String CARRIER_TABLE = "CarrierInfo";
	final static String IP_TABLE = "IPPrefix";

	private final static int NUMBER = 1;
	private final static int LOCATION = 2;
	private final static int CARRIER = 3;
	private final static int IPPREFIX = 4;

	private final static String AUTHORITY = CallerLocationProvider.class.getName();
	private CallerLocationAdapter adapter;
	static UriMatcher matcher;
	static {
		matcher = new UriMatcher(UriMatcher.NO_MATCH);
		matcher.addURI(AUTHORITY, "CallerLoc", NUMBER);
		matcher.addURI(AUTHORITY, "LocationInfo", LOCATION);
		matcher.addURI(AUTHORITY, "CarrierInfo", CARRIER);
		matcher.addURI(AUTHORITY, "IPPrefix", IPPREFIX);
	}

	public static Uri NUMBER_URI = Uri.parse("content://" + AUTHORITY + "/"
			+ "CallerLoc");
	public static Uri LOCATION_URI = Uri.parse("content://" + AUTHORITY + "/"
			+ "LocationInfo");
	public static Uri CARRIER_URI = Uri.parse("content://" + AUTHORITY + "/"
			+ "CarrierInfo");
	public static Uri IP_URI = Uri.parse("content://" + AUTHORITY + "/"
			+ "IPPrefix");

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		return 0;
	}

	@Override
	public String getType(Uri uri) {
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		return null;
	}

	@Override
	public boolean onCreate() {
		adapter = new CallerLocationAdapter(getContext());
		return (adapter == null) ? false : true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		Cursor cursor = null;
		SQLiteQueryBuilder sql = new SQLiteQueryBuilder();
		SQLiteDatabase db = null;
		int match = matcher.match(uri);
		Log.d(TAG, "query, uri is " + match);
		switch (match) {
		case NUMBER: {
			sql.setTables(NUMBER_TABLE);
		}
			break;
		case LOCATION: {
			sql.setTables(LOCATION_TABLE);
		}
			break;
		case CARRIER: {
			sql.setTables(CARRIER_TABLE);
		}
			break;
		case IPPREFIX: {
			sql.setTables(IP_TABLE);
		}
			break;
		default: {
			throw new IllegalArgumentException("CallerLocationProvider, unknown URI " + uri);
		}
		}
		db = adapter.open();
		if (null == db) {
			Log.e(TAG, "db open failed!");
			return null;
		}
		String orderBy = sortOrder;
		if (null == sortOrder || TextUtils.isEmpty(sortOrder)) {
		}
		try {
			cursor = sql.query(db, projection, selection, selectionArgs, null,null, orderBy);
		} catch (Exception e) {
			Log.e(TAG, "query failed with " + e.toString());
		}
		return cursor;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,String[] selectionArgs) {
		return 0;
	}

}
